{extend name="common/default" /}

{block name="style"}
<style>
    .avatar {
        border-radius: 50%;
        width: 40%;
        max-width: 150px;
        max-height: 150px;
        margin: auto;
    }

    /*body {*/
        /*height: 100%;*/
        /*width: 100%;*/
        /*background: url({$config.background});*/
        /*background-size: cover;*/
        /*background-position: center;*/
        /*background-repeat: no-repeat;*/
        /*background-attachment: fixed;*/
    /*}*/

    .container {
        width: 100%;
        height: 100%;
        background: rgba(255, 255, 255, 0.62);
    }
</style>
{/block}


{block name="container"}
<div class="container" id="container">
    <div class="page js_show">
        <div class="page__hd">
            <h1 class="page__title"></h1>
            <p class="page__desc"></p>
        </div>
        <div class="page__bd page__bd_spacing" style="text-align: center;">
            <img class="avatar" src="{$rexam_user.sysuser.avatar|cdnurl}"/>
            <h2 style="color:#fff">{$rexam_user.sysuser.nickname}</h2>

            <div class="weui-cell weui-cell_active">
                <div class="weui-cell__hd"><label class="weui-label">姓名</label></div>
                <div class="weui-cell__bd">
                    <input class="weui-input" placeholder="请填写真实姓名" value="{$rexam_user.name}" name="user_name" type="text">
                </div>
            </div>
            <div class="weui-cell weui-cell_active group-bind">
                <div class="weui-cell__hd"><label class="weui-label">群组</label></div>
                <div class="weui-cell__bd">
                    <input class="weui-input" placeholder="请选择您所在群组" value="{$rexam_group.name|default=''}" name="group_name" type="text" disabled="disabled">
                    <input type="hidden" name="group_id" value="{$rexam_group.id|default='0'}"/>
                </div>
            </div>
            <h2>&nbsp;</h2>
            <div class="weui-form__opr-area">
                <a class="weui-btn weui-btn_primary weui-btn_disabled" href="javascript:;" id="submit">确定</a>
            </div>
        </div>
    </div>
</div>
{/block}

{block name="javascript"}
<script>
    var pid = "{$pid}";
    var picked = false;

    function check_fill(){
        var fill = 0;
        var $input = $('input');
        $input.each(function(){
            if($(this).val())fill++;
        });
        console.log(fill,$input.length);
        if(fill == $input.length){
            $('#submit').removeClass('weui-btn_disabled');
        }else{
            $('#submit').addClass('weui-btn_disabled');
        }
    }

    function submit() {
        var $input = $('input');
        var data = {};
        $input.each(function(){
            eval("data." + $(this).attr('name') + "='"+$(this).val()+"'");
        });
        $.ajax({
            type: 'POST',
            url: "{:addon_url('rexam/user/group')}",
            data: data,
            dataType: 'json',
            success: function (res) {
                if (res.code == 1) {
                    window.location.href = "{:addon_url('rexam/index/index')}";
                }
            }
        })
    }

    function pick(data){
        picked = false;
        weui.picker(data, {
            className: 'custom-classname',
            onChange: function (result) {
                console.log(result);
            },
            onConfirm: function (result) {
                pid = result[0].value;
                picked = true;
            },
            onClose: function () {
                if(picked){
                    finger();
                }
            },
            id: 'picker',
            title: '请选择',
        });
    }

    function finger() {
        $.ajax({
            type: 'POST',
            url: "{:addon_url('rexam/group/index')}",
            data: {
                pid: pid,
            },
            dataType: 'json',
            success: function (res) {
                console.log(res);
                if(res.data.total>0){
                    pick(res.data.rows);
                }else{
                    $("input[name='group_name']").val(res.data.one.name);
                    $("input[name='group_id']").val(res.data.one.id);
                }
                check_fill();
            }
        })
    }

    $(function () {
        var $input = $('input');
        $input.on('input', function(){
            check_fill();
        });

        $('.group-bind').click(function(){
            pid = 1;
            finger();
        });

        $('#submit').on('click', function() {
            if ($(this).hasClass('weui-btn_disabled')) return;
            submit();
        });

        check_fill();
    });
</script>
{/block}